/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package top.shaozuo.simple.mybatis.annotation;

import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;

/**
 * TODO Comment
 *
 * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
 * @since TODO Date : 2021-05-06
 */
public class MybatisBeanDefinitionRegistrar
        implements ImportBeanDefinitionRegistrar, EnvironmentAware {

	private Environment environment;

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
	        BeanDefinitionRegistry registry) {
		AnnotationAttributes mapperScanAttrs = AnnotationAttributes.fromMap(
		        importingClassMetadata.getAnnotationAttributes(EnableMybatis.class.getName()));
		if (mapperScanAttrs != null) {
			registerBeanDefinitions(mapperScanAttrs, registry);
		}
	}

	void registerBeanDefinitions(AnnotationAttributes annoAttrs, BeanDefinitionRegistry registry) {
		BeanDefinitionBuilder beanDefinitionBuilder = genericBeanDefinition(
		        SqlSessionFactoryBean.class);

		/**
		 * <bean id="sqlSessionFactory" class=
		 * "org.mybatis.spring.SqlSessionFactoryBean">
		 * <property name="dataSource" ref="dataSource" />
		 * <property name="mapperLocations" value="classpath*:" /> </bean >
		 */
		beanDefinitionBuilder.addPropertyReference("dataSource",
		        (String) annoAttrs.get("dataSource"));
		// Spring String 类型可以自动转化 Spring Resource
		if (StringUtils.hasText(annoAttrs.getString("configLocation"))) {
			beanDefinitionBuilder.addPropertyValue("configLocation",
			        annoAttrs.getString("configLocation"));
		}
		if (annoAttrs.getStringArray("mapperLocations") != null) {
			beanDefinitionBuilder.addPropertyValue("mapperLocations",
			        annoAttrs.getStringArray("mapperLocations"));
		}
		if (StringUtils.hasText(annoAttrs.getString("environment"))) {
			beanDefinitionBuilder.addPropertyValue("environment",
			        annoAttrs.getString("environment"));
		}
		// 自行添加其他属性
		if (StringUtils.hasText(annoAttrs.getString("typeHandlersPackage"))) {
			beanDefinitionBuilder.addPropertyValue("typeHandlersPackage",
			        annoAttrs.getString("typeHandlersPackage"));
		}

		// SqlSessionFactoryBean 的 BeanDefinition
		BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
		String beanName = (String) annoAttrs.get("value");
		registry.registerBeanDefinition(beanName, beanDefinition);
	}

	private Object resolvePlaceholder(Object value) {
		if (value instanceof String) {
			return environment.resolvePlaceholders((String) value);
		}
		return value;
	}

	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}
}
